import { reactive } from "vue";
import model from "./model";
import { copyModel, formatDateTimes } from "@/utils/index";
import { $getMessageList } from "@/utils/api";
const MODEL = copyModel(model);
const hashChannel = {
  0: "没有管道",
  1: "管道一",
  2: "管道二",
  3: "管道三",
  4: "管道四",
  5: "管道五",
  6: "管道六",
  7: "管道七",
  8: "管道八",
  9: "管道九",
};
const getMessage = async () => {
  model.start = formatDateTimes(model.start);
  model.end = formatDateTimes(model.end);
  if (Array.isArray(model.facilities_id)) {
    model.facilities_id = model.facilities_id[1];
  }
  const res = await $getMessageList(model);
  options.total = res.results.total;
  options.options.tableData = res.results.data.map((item) => {
    return {
      title: item.title || "-",
      time: item.time || "-",
      msg_name: item.msg_name || "-",
      msg: item.msg || "-",
      logourl: item.logourl,
      Channel: hashChannel[item.Channel],
    };
  });
};
getMessage();
const options = reactive({
  className: "kdcrud",
  config: {
    labelWidth: "120px",
    labelPosition: "left",
    formRef: "formLogin",
    formItemArr: [
      {
        id: 1,
        name: "datePicker",
        options: {
          modelName: "start",
          placeholder: "开始日期",
          size: "small",
        },
      },
      {
        id: 2,
        name: "datePicker",
        options: {
          modelName: "end",
          placeholder: "结束日期",
          size: "small",
        },
      },
      {
        id: 3,
        name: "select",
        options: {
          modelName: "messageType",
          optionArr: [],
          placeholder: "选择警告类型",
          size: "small",
        },
      },
      {
        id: 4,
        name: "cascader",
        options: {
          modelName: "facilities_id",
          optionArr: [],
          placeholder: "选择警告类型",
          size: "small",
        },
      },
      {
        id: 9,
        name: "button",
        options: {
          buttonText: "搜索",
          onclick: async () => {
            getMessage();
          },
        },
      },
      {
        id: 10,
        name: "button",
        options: {
          buttonText: "重置",
          onclick: () => {
            Object.assign(model, MODEL);
          },
        },
      },
    ],
  },
  options: {
    tableData: [],
    height: 580,
    size: "small",
    optionsArr: [
      {
        prop: "title",
        label: "标题",
        width: 280,
      },
      {
        prop: "time",
        label: "时间",
      },
      {
        prop: "msg_name",
        label: "消息类型",
        width: 180,
      },
      {
        prop: "msg",
        label: "消息内容",
        width: 180,
      },
      {
        prop: "logourl",
        label: "图片",
        width: 180,
        slot(scoped) {
          return `<img src=${scoped.row.logourl} />`;
        },
      },
      {
        prop: "Channel",
        label: "管道",
      },
    ],
  },
  background: true,
  layout: "prev, pager, next",
  total: 3500,
  currentChange(e) {
    console.log(e);
    model.pageIndex = e;
    getMessage();
  },
});
export default options;
